package org.Image.management.SpringDataService.Service.Sys_User.controller;


import jakarta.validation.Valid;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.Image.management.SpringDataService.Pojo.Exception.BusinessException;
import org.Image.management.SpringDataService.Pojo.Exception.StatusCode;
import org.Image.management.SpringDataService.Pojo.Result.Result;
import org.Image.management.SpringDataService.Service.Sys_User.Pojo.Vo.UpdateVo;
import org.Image.management.SpringDataService.Service.Sys_User.Pojo.dto.LoginRequest;
import org.Image.management.SpringDataService.Service.Sys_User.Pojo.dto.RegisterRequest;
import org.Image.management.SpringDataService.Service.Sys_User.Pojo.dto.UpdatePasswordRequest;
import org.Image.management.SpringDataService.Service.Sys_User.Pojo.dto.UpdateUserNameRequest;
import org.Image.management.SpringDataService.Service.Sys_User.Pojo.mapper.User;
import org.Image.management.SpringDataService.Service.Sys_User.Service.impl.UserServiceInterface;
import org.Image.management.utils.JsonFileUtils;
import org.springframework.stereotype.Controller;

@Controller
@RequiredArgsConstructor
@Slf4j
// 下面 用户类 放行
public class UserController {
    private final UserServiceInterface userServiceInterface;

    // 登录端点
    public Result<?> login(@Valid LoginRequest loginRequest) {

        try {
            User user = userServiceInterface.loginUser(loginRequest);
            return Result.success(user);

        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

    // 注册端点
    public Result<?> register(@Valid RegisterRequest registerRequest) {
        JsonFileUtils.saveJsonToFile("注册端点", registerRequest);
        try {
            User user = userServiceInterface.registerUser(registerRequest);
            return Result.success(user);

        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

    /**
     * 修改密码
     *
     * @param updatePasswordRequest {
     *                              oldPassword 旧邮箱
     *                              newPassword 新邮箱
     *                              }
     * @return 统一返回
     */
    public Result<?> modifyPassword(@Valid UpdatePasswordRequest updatePasswordRequest) {
        log.info("密码{}",updatePasswordRequest);
        try {
            UpdateVo updateVo = userServiceInterface.modifyPassword(
                    updatePasswordRequest.getUserId(),
                    updatePasswordRequest.getOldPassword(),
                    updatePasswordRequest.getNewPassword()
            );
            return Result.success(updateVo);

        } catch (Exception e) {

            return Result.failure(e.getMessage());
        }
    }

    /**
     * 修改用户名
     *
     * @param updateUserNameRequest {
     *                              oldUserName 旧用户名
     *                              newUserName 新用户名
     *                              }
     * @return 统一返回
     */
    public Result<?> modifyUserName(@Valid UpdateUserNameRequest updateUserNameRequest) {
        try {
            UpdateVo updateVo = userServiceInterface.modifyUserName(updateUserNameRequest.getUserId(), updateUserNameRequest.getNewUserName());
            return Result.success(updateVo);

        } catch (Exception e) {
            return Result.failure(e.getMessage());
        }
    }

    /**
     * 删除用户
     */
    public Result<?> deleteUser(Long userId) {
        try {
            boolean deleteUser = userServiceInterface.deleteUser(userId, true);
            return Result.success(deleteUser);

        } catch (Exception e) {
            return Result.failure(e.getMessage());
        }
    }
    /**
     * 删除用户
     */
    public Result<?> deleteUser(LoginRequest loginRequest) {
        try {
            log.info("删除用户{}",loginRequest);
            boolean deleteUser = userServiceInterface.deleteUser(loginRequest);

            return Result.success(deleteUser);

        } catch (Exception e) {

            return Result.failure(e.getMessage());
        }
    }
}
